﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WEBDT.Base;
using WEBDT.ViewModel;

namespace WEBDT.Areas.Admin.Controllers
{
    public class HoaDonController : BaseController<HoaDon>
    {
        //
        // GET: /Admin/HoaDon/

        public ActionResult Index()
        {
            return View();
        }
        [HttpGet]
        public override ActionResult insert()
        {
            var model = new HoaDonModel
            {
                tontai = false,
            };
            ViewData["username"] = new SelectList(repository.Danhsach<Account>(), "username", "username");
            return View(model);
        }

        [HttpPost]
        public override ActionResult insert(FormCollection fr)
        {
            var ht = new HoaDonModel
            {
                Username = fr.Get("username"),
                //TongTien = Convert.ToInt32(fr.Get("tongtien")),
                NgayMua = fr.Get("ngaymua"),
                Status = Convert.ToInt32(fr.Get("status")),
            };
            var model = new HoaDon
            {
                Username = ht.Username,
                TongTien = 0,
                NgayMua = ht.NgayMua,
                Status = ht.Status,
            };
            repository.Insert<HoaDon>(model);
          
            return RedirectToAction("getall");
            //return Redirect("../cthoadon/insert");
        }

        [HttpGet]
        public override ActionResult update(int id)
        {
            var ht = repository.SelectByID<HoaDon>(id);
            var model = new HoaDonModel
            {
                MaHD = ht.MaHD,
                Username = ht.Username,
                TongTien = Convert.ToInt32(ht.TongTien),
                NgayMua = ht.NgayMua,
                Status = Convert.ToInt32(ht.Status),
            };
            ViewData["username"] = new SelectList(repository.Danhsach<Account>(), "username", "username");
            return View(model);
        }

        [HttpPost]
        public override ActionResult update(FormCollection fr)
        {
            var httt = new HoaDonModel
            {
                MaHD = Convert.ToInt32(fr.Get("mahd")),
                Username = fr.Get("username"),
                TongTien = Convert.ToInt32(fr.Get("tongtien")),
                NgayMua = fr.Get("ngaymua"),
                Status = Convert.ToInt32(fr.Get("status")),
            };

            var model = repository.SelectByID<HoaDon>(httt.MaHD);
            model.Username = httt.Username;
            model.TongTien = httt.TongTien;
            model.NgayMua = httt.NgayMua;
            model.Status = httt.Status;
            repository.commitchanges();
            return RedirectToAction("getAll");
        }
    }
}
